<?php

namespace excel;

class phpExcel
{

    function readExcle($file){
        if(empty($file)){
            return FALSE;
        }

        if(!file_exists($file)){
            return FALSE;
        }
        $objPHPExcelReader = \PHPExcel_IOFactory::load($file);  //加载excel文件

        $data = array();

        foreach($objPHPExcelReader->getWorksheetIterator() as $sheet)  //循环读取sheet
        {
            foreach($sheet->getRowIterator() as $row)  //逐行处理
            {
                if($row->getRowIndex()<2)  //确定从哪一行开始读取
                {
                    continue;
                }
                $tmp = array();

                foreach($row->getCellIterator() as $cell)  //逐列读取
                {


                    $tdata = $cell->getValue(); //获取cell中数据

                    if(is_object($tdata)) {
                        $tdata = $tdata->__toString();
                    }


                    $tmp[] = $tdata;

                }

                if(!empty($tmp)){
                    $data[] = $tmp;
                }

            }
        }

        return $data;



    }


    function toExcle($data)
    {

        $title = $data['title'];
        $header = $data['header'];
        $body = $data['body'];

        $objExcel = new \PHPExcel();

        $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
        // $objExcel->getActiveSheet()->setTitle($title);

        $count = count($header);

        // 设置水平垂直居中
        $objExcel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objExcel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);

        // 字体和样式
        $objExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(12);

        for ($i = 65; $i < $count + 65; $i++) {

            $objExcel->getActiveSheet()->getStyle($this->getChar($i) . '1')->getFont()->setBold(true);
            $objExcel->getActiveSheet()->getStyle($this->getChar($i) . '1')->getFont()->setSize(12);
        }

        // 第一行、第二行的默认高度
        $objExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);

        //设置某一列的宽度
        for ($i = 65; $i < $count + 65; $i++) {
            $width = 200;
            if (!empty($header[$i - 65]['width'])) {
                $width = $header[$i - 65]['width'];
            }
            $objExcel->getActiveSheet()->getColumnDimension($this->getChar($i))->setWidth($width);
        }

        //设置表头
        $objActSheet = $objExcel->getActiveSheet(0);
        $objActSheet->setTitle($title);//设置excel的标题

        for ($i = 65; $i < $count + 65; $i++) {
            $objActSheet->setCellValue($this->getChar($i) . '1', $header[$i - 65]['name']);
        }

        $baseRow = 2; //数据从N-1行开始往下输出 这里是避免头信息被覆盖
        foreach ($body as $r => $d) {

            for ($i = 65; $i < $count + 65; $i++) {
                $objExcel->getActiveSheet()->setCellValue($this->getChar($i) . $baseRow, $d[$i - 65]);
            }
            $baseRow++;

        }


        $objExcel->setActiveSheetIndex(0);

        //4、输出

        header('Content-type: application/vnd.ms-excel;charset=utf-8;name="' . $title . '.xls"');
        header("Content-Disposition: attachment; filename=$title.xls");
        header('Cache-Control: max-age=0');
        //header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        //header ('Cache-Control: cache, must-revalidate');
        header('Pragma: public');

        $objWriter->save('php://output');
        die();
    }


    function getChar($i)
    {
        return strtoupper(chr($i));
    }
}